package com.rslai.commons.validator.jsonvalidator.validators;

import com.rslai.commons.validator.jsonvalidator.matchers.JsonAssert;
import com.rslai.commons.validator.jsonvalidator.schema.Schema;
import org.hamcrest.core.Is;

import java.util.List;

/**
 * JSON校验器，有序数组校验器
 */
public class OrderValidator extends ArrayValidator {
    public static final String VALIDATOR_NAME = "order";

    /**
     * 构造函数
     * @param list list
     */
    public OrderValidator(List<Object> list) {

    }

    /**
     * 数组校验
     * @param schemas schemas
     * @param actual 实际
     */
    @Override
    protected void arrayValidate(List<Schema<Object>> schemas, List<Object> actual) {
        JsonAssert.assertThat("实际传入的数组大小与期望的数组大小不等: " + getCurrentKeyPath(), actual.size(), Is.is(schemas.size()));
        for (int i = 0; i < schemas.size(); i++) {
            Schema<Object> schema = schemas.get(i);
            schema.validate(getCurrentKeyPath() + "[" + i + "]", actual.get(i));
        }
    }

    /**
     * 返回校验名称
     * @return 校验名称
     */
    @Override
    public String name() {
        return VALIDATOR_NAME;
    }
}